package com.xiaoyu.util2;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;

@SuppressWarnings({ "unchecked", "rawtypes" })
public class SysUtil {
	public static String STR_IP = "";

	public static String STR_HOSTNAME = "";

	static {
		try {
			InetAddress addr = InetAddress.getLocalHost();
			STR_HOSTNAME = addr.getHostName();
			Enumeration nets = NetworkInterface.getNetworkInterfaces();
			ArrayList<NetworkInterface> networkInterfaceList = Collections.list(nets);
			for (NetworkInterface netint : networkInterfaceList)
				if (null != netint.getHardwareAddress()) {
					List<InterfaceAddress> list = netint.getInterfaceAddresses();
					for (InterfaceAddress interfaceAddress : list) {
						InetAddress ip = interfaceAddress.getAddress();
						if (ip instanceof Inet4Address)
							STR_IP += interfaceAddress.getAddress().toString();
					}
				}
		} catch (Exception e) {
			System.out.println("获取服务器IP出错");
		}
	}
}